define(window.backboneCommon.getViewDefine("targets.area"), function ($,require,Collection,tpl) {
    var defaultsOptions={
        dataType:"default",//default | group
        btnSubmitText:"确定",
        checkEmpty:false,
        submitCallback:null
    };
    var pagePrivate = {
        getDefaults :function () {
            return defaultsOptions
        },
        getOptions : function (options) {
            options=$.ExtCommon.isCheckEmpty(options)?{}:options;
            return $.ExtCommon.initMergeJson(this.getDefaults(),options);
        }
    };
    var backboneView= Backbone.View.extend({
        events: {
            "hidden.bs.modal #modal_targetArea": "hideModalCallback",
            "click #submit_modal_targetArea": "saveSubmit"
        },
        initialize: function () {
            this.listenTo(this.model, 'bind:AreaDefault', this.bindAreaDefault);
            this.listenTo(this.model, 'bind:AreaGroup', this.bindAreaGroup);
            this.render();
        },
        render:function(){
            var templateHtml=_.template(tpl);
            this.$el.html(templateHtml());
            return this;
        },
        initMain:function(options){
            this.elementModel={
                pArea:$('#panel_modal_targetArea'),
                btnSubmit:$("#submit_modal_targetArea"),
                modal:$('#modal_targetArea')
            };
            this.extObject={};
            this.pageData={initPage:false};
            this.pageOptions=$.ExtCommon.initMergeJson(pagePrivate.getOptions(),options);
            this.pageData.lastDataType=this.pageOptions.dataType;
            this.elementModel.btnSubmit.text(this.pageOptions.btnSubmitText);
        },
        initPage:function (json,dataType) {
            this.pageOptions.dataType=dataType||"default";
            this.pageData.listSelect=json;
            if(!this.pageData.initPage){
                this.pageData.initPage=true;
                this._initClear();
            }
            if(this.pageData.lastDataType!==this.pageOptions.dataType){
                this.extObject.area=null;
                this._initClear();
                this.pageData.lastDataType=this.pageOptions.dataType;
            }
            if(this.extObject.area){
                this._showModal();
                this.extObject.area.setSelected(this.pageData.listSelect);
            }else{
                this._getAreaData();
            }
        },
        _initClear:function () {
            if(this.elementModel.area){this.elementModel.area.remove()}
            this.elementModel.pArea.html("<div></div>");
            this.elementModel.area=this.elementModel.pArea.children();
        },
        _getAreaData:function () {
            switch(this.pageOptions.dataType){
                case "default":
                    this.collection.getAreaDefault();
                    break;
                case "group":
                    this.collection.getAreaGroup();
                    break;
            }
        },
        bindAreaDefault:function () {
            var self=this;
            var res=self.model.get("dataAreaDefault");
            self.extObject.area=self.elementModel.area.ExtSelectedMove({
                attrName:"area",placeholder:"请输入省份、城市名称",initValue:self.pageData.listSelect, data:res.data,
                initCallback:function(){self._showModal();}
            });
        },
        bindAreaGroup:function () {
            var self=this;
            var res=self.model.get("dataAreaGroup");
            self.extObject.area=self.elementModel.area.ExtSelectedMove({
                attrName:"area",placeholder:"请输入省份、城市名称",initValue:self.pageData.listSelect, data:res.data,
                isMergeParent:false,isTopParentEqual:true,
                initCallback:function(){self._showModal();}
            });
        },
        _checkForm:function () {},
        saveSubmit:function () {
            var self=this;
            self._hideModal();
            var jsonData= this.extObject.area.getSelectedList();
            if(self.pageOptions.checkEmpty && $.ExtCommon.isCheckEmpty(jsonData)){
                $.ExtSweetAlert.confirm({
                    confirmButtonText: "是",
                    cancelButtonText:"否",
                    title:"是否批量删除地域定向？",
                    infoExt:{
                        confirmCallback:function(cb){
                            if(self.pageOptions.submitCallback){
                                self.pageOptions.submitCallback(jsonData,function(isSuccess){
                                    cb(true);
                                    if(!isSuccess){ self._showModal();}
                                });
                            }
                        },
                        cancelCallback:function(){
                            self._showModal();
                        }
                    }
                });
            }else{
                if(this.pageOptions.submitCallback){this.pageOptions.submitCallback(jsonData)}
            }
        },
        _showModal:function(){
            this.elementModel.modal.modal("show");
            $.ExtAssist.modal.computeTop(this.elementModel.modal);
        },
        _hideModal: function () {
            this.elementModel.modal.modal("hide");
        },
        hideModalCallback:function () {
            this.extObject.area.clearSelected();
        }
    });
    var viewModel=function(options){
        this.collection= new Collection();
        this.model=this.collection.model;
        this.view=new backboneView({el:options.pElement,collection:this.collection,model:this.model});
        this.view.initMain(options);
    };
    viewModel.prototype={
        init:function (json,dataType) {
            this.view.initPage(json,dataType);
        }
    };
    return viewModel;
});